package model;


import java.io.Serializable;

import java.util.ArrayList;

import java.util.Date;
import java.util.List;
import java.util.Map;

import model.UserRelationShip.UserRelationState;


import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import utils.Functions;

public class User implements Serializable {
	
	
	private static final long serialVersionUID = -7454594263311743723L;
	private int ID;
	private String name;
	private String password;
	private String email;
	private List<UserRelationShip> relationShips;

	private List<Poll> polls = new ArrayList<Poll>();
	private Date signupDate;
	private Date lastConnectDate;

	public User()
	{
		this.ID = 0;
		this.name = "";
		this.password = "";
		this.email = "";
		this.relationShips = new ArrayList<UserRelationShip>();
	}

	public User(String name, String password, String email) {
	   
		this.ID = 0;
		this.name = name;
		this.password = password;
		this.email = email;
		this.signupDate = Functions.now();
		this.lastConnectDate = Functions.now();
		this.relationShips = new ArrayList<UserRelationShip>();
		setRelationShips(relationShips);
	}
	public int getID() {
		return ID;
	}
	public void setID(int iD) {
		ID = iD;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public List<UserRelationShip> getRelationShips() {
		return relationShips;
	}

	private void setRelationShips(List<UserRelationShip> relationShips) {
		this.relationShips = relationShips;
	}
	
    
    
    private UserRelationShip getRelationShipWithBuddy(User buddy) {
    	
    	for(UserRelationShip relationShip : getRelationShips())
    	{
    		if(relationShip.getUserA() == buddy || relationShip.getUserB() == buddy)
    		{
    			return relationShip;
    		}
    	}
    	
    	return null;
    }
    
    public UserRelationState getRelationState(User buddy) {
    	
    	UserRelationShip relationShip = getRelationShipWithBuddy(buddy);
    	
    	if(relationShip != null)
    		return relationShip.getRelationState(this);
    	else
    		return null;
    }
    
    public void requestBuddy(User buddy){
    	
    	UserRelationShip newRelationShip = new UserRelationShip(this, UserRelationState.RSENT, buddy, UserRelationState.RPENDING);
    	this.getRelationShips().add(newRelationShip);
    	buddy.getRelationShips().add(newRelationShip);
    }  
 
    public void blockBuddy(User buddy){
    	
    	UserRelationShip relationShip = getRelationShipWithBuddy(buddy);
    	
    	if(relationShip != null)
    		relationShip.setRelationState(this, UserRelationState.BLOCKED);
    }
    
    public void acceptBuddyRequest(User buddy){
    	
    	UserRelationShip relationShip = getRelationShipWithBuddy(buddy);
    	
    	if(relationShip != null) {
    		relationShip.setRelationState(this, UserRelationState.OK);
    		relationShip.setRelationState(buddy, UserRelationState.OK);
    	}
    	
    	//TODO or maybe work with the messages; ask Martina's opinion 
    }
    
     /* TODO martina
     * @param buddy
     * @return
     */
    public Boolean transferFile(User buddy){
    	// transfer directly without using server
    	
    	
    	throw new NotImplementedException();
    }
    
    public String toString() {
		return "["+ID+";"+name+";"+password+";"+email+"]";
	}
    public boolean equals(Object arg0) {
		if (arg0 instanceof User) {
			User user = (User) arg0;
			return this.ID == user.ID 
			&& (this.name != null) ? this.name.equals(user.name) : user.name == null
			&& (this.email != null) ? this.email.equals(user.email) : user.email == null
			&& (this.password != null) ? this.password.equals(user.password) : user.password == null;
		}
		else
			return false;
	}

	public String getBuddyState(User buddy) {
		
		UserRelationShip relationShip = getRelationShipWithBuddy(buddy);
		
		if(relationShip != null) 
			return relationShip.getRelationState(buddy).toString();
		else
			return null;
	}
	public void createPoll(String question, Map<String, Integer> responses){
	    Poll poll = new Poll(question,responses);
		this.polls.add(poll);
	}
	public void vote(Poll poll, String response ){
		poll.addVote(response);
	}
	
	public Map<String, Integer> getPoolResults(Poll poll){
		return poll.getResponses();
	}
	
	public List<Poll> getPolls() {
		return polls;
	}

	private void setPolls(List<Poll> polls) {
		this.polls = polls;
	}
	
	public Date getSignupDate() {
		return signupDate;
	}

	public void setSignupDate(Date signupDate) {
		this.signupDate = signupDate;
	}
	
	public Date getLastConnectDate() {
		return lastConnectDate;
	}

	public void setLastConnectDate(Date lastConnectDate) {
		this.lastConnectDate = lastConnectDate;
	}
}
